const express = require('express');
const pool = require('../utils/pool')
let router = express.Router();
router.get('/fuzzySearch',(req,res)=>{
	var obj= req.query
	let sql = 'select * from product'
	let params = []
	
	// 只有当name不为空时才添加模糊查询条件
	if(obj.name && obj.name.trim() !== '') {
		sql += ' where name LIKE ?'
		params.push(`%${obj.name}%`)
	}
	
	pool.query(sql, params, (x,y)=>{
		if(x){
			throw x
		}
		console.log(x);
    console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.get('/classFuzzySearch',(req,res)=>{
	var obj= req.query
	let sql = 'select * from productclass'
	let params = []
	
	// 只有当name不为空时才添加模糊查询条件
	if(obj.name && obj.name.trim() !== '') {
		sql += ' where name LIKE ?'
		params.push(`%${obj.name}%`)
	}
	
	pool.query(sql, params, (x,y)=>{
		if(x){
			throw x
		}
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
	})
})
router.get('/picSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from pic',(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.post('/picAdd',(req,res)=>{
  let obj = req.body
  console.log(obj);
	pool.query('insert into pic set ?',[obj],(x,y)=>{
		if(x){
			throw x;
		}
		console.log(y);
		res.send({code:200,msg:'注册成功'})
	})
 
});
router.get('/picDelete',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('delete from pic where pid = ?',[obj.pid],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'该员工不存在'})
		}else{
			res.send({code:200,msg:'删除成功'})
		}
		
	})
	
})
router.get('/picIdSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from pic where pid = ?',[obj.pid],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.get('/picStatusSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from pic where status = ?',[obj.status],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.post('/picUpdate',(req,res)=>{
	var obj = req.body
	console.log(obj);
	pool.query('update pic set  ? where pid = ?',[obj,obj.pid],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功',date:obj})
		}
		
	})
	
})
router.get('/productClassSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from productClass',(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.get('/classNameSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from productClass where name = ?',[obj.name],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.get('/classCodeSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from productClass where code = ?',[obj.code],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.post('/productClassAdd',(req,res)=>{
  let obj = req.body
  console.log(obj);
	pool.query('insert into productClass set ?',[obj],(x,y)=>{
		if(x){
			throw x;
		}
		console.log(y);
		res.send({code:200,msg:'注册成功'})
	})
 
});
router.get('/productClassDelete',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('delete from productClass where code = ?',[obj.code],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'该员工不存在'})
		}else{
			res.send({code:200,msg:'删除成功'})
		}
		
	})
	
})
router.post('/productClassUpdate',(req,res)=>{
	var obj = req.body
	console.log(obj);
	pool.query('update productClass set  ? where code = ?',[obj,obj.code],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'修改失败'})
		}else{
			res.send({code:200,msg:'修改成功',date:obj})
		}
		
	})
	
})
module.exports=router;